//
// Created by root on 6/22/17.
//
#include "lib.h"

/** general use err_dialog, just pass errmsg. */
void err_dialog(const gchar *errmsg) {
    g_warning(errmsg); /* log to terminal window */

    /* create an error dialog and display modally to the user */
    GtkWidget *dialog = gtk_message_dialog_new(NULL,
                                    GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
                                    GTK_MESSAGE_ERROR,
                                    GTK_BUTTONS_OK,
                                    errmsg);

    gtk_window_set_title(GTK_WINDOW(dialog), "Error!");
    gtk_dialog_run(GTK_DIALOG(dialog));
    gtk_widget_destroy(dialog);
}

/** font_select_dialog used to set textview font.
 *  create a new pango font description and calls
 *  gtk_widget_modify_font to set textview font.
 */
void font_select_dialog(GtkWidget *widget, FunnyWindow *app) {
    GtkWidget *dialog = gtk_font_selection_dialog_new("Select Font");

    /* set initial font name if (!wanted) use default */
    if (!gtk_font_selection_dialog_set_font_name(GTK_FONT_SELECTION_DIALOG(dialog), app->fontname))
        gtk_font_selection_dialog_set_font_name(GTK_FONT_SELECTION_DIALOG(dialog), DEFAULT_FONT_NAME);

    GtkResponseType result = gtk_dialog_run(GTK_DIALOG(dialog));

    if (result == GTK_RESPONSE_OK || result == GTK_RESPONSE_APPLY) {

        if (app->fontname) g_free(app->fontname);

        app->fontname = gtk_font_selection_dialog_get_font_name(GTK_FONT_SELECTION_DIALOG(dialog));

        if (!app->fontname) {
            err_dialog("error: invalid font returned.");
            return;
        }

        PangoFontDescription *font_desc = pango_font_description_from_string(app->fontname);

        gtk_widget_modify_font(app->view, font_desc);
        pango_font_description_free(font_desc);
    }
    gtk_widget_destroy(dialog);
}

void buffer_file_insert_dialog(FunnyWindow *app, gchar *filename) {
    /* Create a new file chooser widget */
    GtkWidget *dialog = gtk_file_chooser_dialog_new("Select a file for editing",
                                         GTK_WINDOW(app->window),
                                         GTK_FILE_CHOOSER_ACTION_OPEN,
                                         GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                                         GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
                                         NULL);

    if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
        filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
        buffer_insert_file(app, filename);
    }

    gtk_widget_destroy(dialog);
}

void buffer_file_open_dialog(FunnyWindow *app, gchar *filename) {
    /* Create a new file chooser widget */
    GtkWidget *dialog = gtk_file_chooser_dialog_new("Select a file for editing",
                                         GTK_WINDOW(app->window),
                                         GTK_FILE_CHOOSER_ACTION_OPEN,
                                         GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                                         GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
                                         NULL);

    if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
        app->filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
        split_fname(app);
        buffer_insert_file(app, NULL);
    }

    gtk_widget_destroy(dialog);
}

/* Removed until upstream bug fixed
 * see: https://bugzilla.gnome.org/show_bug.cgi?id=729800 and
 * https://bugzilla.gnome.org/show_bug.cgi?id=779605
 */
/*
void file_open_recent_dlg (FunnyWindow *app)
{
    GtkWidget *dialog;

    dialog = gtk_recent_chooser_dialog_new ("Recent Documents",
                                            GTK_WINDOW (app->window),
                                            GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                                            GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
                                            NULL);
    if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
    {
        GtkRecentInfo *info;
        gchar *filename;

        info = gtk_recent_chooser_get_current_item (GTK_RECENT_CHOOSER (dialog));
        filename = uri_to_filename (gtk_recent_info_get_uri (info));
        app_free_filename (app);
        app->filename = g_strdup (filename);
        split_fname (app);
        gtk_recent_info_unref (info);

        buffer_insert_file (app, NULL);
    }
    gtk_widget_destroy (dialog);
}
*/
void dialog_info(const gchar *msg, const gchar *title) {
    GtkWidget *dialog = gtk_message_dialog_new(NULL,
                                    GTK_DIALOG_MODAL |
                                    GTK_DIALOG_DESTROY_WITH_PARENT,
                                    GTK_MESSAGE_INFO,
                                    GTK_BUTTONS_CLOSE,
                                    msg);

    gtk_window_set_title(GTK_WINDOW(dialog), title);
    gtk_dialog_run(GTK_DIALOG(dialog));
    gtk_widget_destroy(dialog);
}

gboolean dialog_yes_no_msg(const gchar *msg, const gchar *title, gboolean default_return) {
    gboolean ret = default_return ? TRUE : FALSE;
    GtkWidget *dialog = gtk_message_dialog_new(NULL,
                                    GTK_DIALOG_MODAL |
                                    GTK_DIALOG_DESTROY_WITH_PARENT,
                                    GTK_MESSAGE_QUESTION,
                                    GTK_BUTTONS_YES_NO,
                                    msg);

    gtk_window_set_title(GTK_WINDOW(dialog), title);
    if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_NO) {
        ret = FALSE;    /* don't save */
    } else {
        ret = TRUE;     /* save */
    }

    gtk_widget_destroy(dialog);

    return ret;
}

gboolean buffer_prompt_on_mod(FunnyWindow *app) {
    gboolean ret = FALSE;
    GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(app->view));

    if (gtk_text_buffer_get_modified(buffer) == TRUE) {
        const gchar *msg = "Do you want to save the changes you have made?";
        GtkWidget *dialog = gtk_message_dialog_new(NULL,
                                        GTK_DIALOG_MODAL |
                                        GTK_DIALOG_DESTROY_WITH_PARENT,
                                        GTK_MESSAGE_QUESTION,
                                        GTK_BUTTONS_YES_NO,
                                        msg);

        gtk_window_set_title(GTK_WINDOW(dialog), "Save?");
        if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_NO) {
            ret = FALSE;    /* don't save */
        } else {
            ret = TRUE;     /* save */
        }

        gtk_widget_destroy(dialog);
    }

    return ret;
}

gchar *get_open_filename(FunnyWindow *app) {
    gchar *filename = NULL;

    GtkWidget *chooser = gtk_file_chooser_dialog_new("Open File...",
                                          GTK_WINDOW(app->window),
                                          GTK_FILE_CHOOSER_ACTION_OPEN,
                                          GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                                          GTK_STOCK_OPEN, GTK_RESPONSE_OK,
                                          NULL);

    if (app->filename) {
        /* set current file path beginning choice */
        gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(chooser), app->fpath);
        /* set current filename beginning choice */
        gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(chooser), app->filename);
    }

    if (gtk_dialog_run(GTK_DIALOG(chooser)) == GTK_RESPONSE_OK) {
        filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(chooser));
    }
    gtk_widget_destroy(chooser);

    return filename;
}

gchar *get_save_filename(FunnyWindow *app) {
    gchar *filename = NULL;

    GtkWidget *chooser = gtk_file_chooser_dialog_new("Save File...",
                                          GTK_WINDOW(app->window),
                                          GTK_FILE_CHOOSER_ACTION_SAVE,
                                          GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                                          GTK_STOCK_SAVE, GTK_RESPONSE_OK,
                                          NULL);

    gtk_file_chooser_set_create_folders(GTK_FILE_CHOOSER(chooser), TRUE);
    gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(chooser), TRUE);
    if (app->filename) {
        /* set current file path beginning choice */
        gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(chooser), app->fpath);
        /* set current filename beginning choice */
        gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(chooser), app->filename);
    }

    if (gtk_dialog_run(GTK_DIALOG(chooser)) == GTK_RESPONSE_OK) {
        filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(chooser));
    }
    gtk_widget_destroy(chooser);

    return filename;
}
